package com.changgou.web.gateway.filter;

import com.changgou.web.gateway.service.AuthService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class AuthFilter implements GlobalFilter,Ordered {

    @Autowired
    private AuthService authService;


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();

        //1.如果是登录，放行
        String path = request.getURI().getPath();
        if ("/api/oauth/login".equals( path ) || !UrlFilter.hasAuthorize( path )){
            return chain.filter( exchange );
        }
        //2.从cookie中获取jti的值，如果没有拒绝访问
        String jti = authService.getJtiFromCookie(request);
        System.out.println(jti);
        if (StringUtils.isEmpty(jti)){
            response.setStatusCode( HttpStatus.UNAUTHORIZED );
            return response.setComplete();
        }
        //3.从redis中获取jwt，如果没有拒绝访问
        String jwt = authService.getJwtFromRedis(jti);
        System.out.println(jwt);
        if (StringUtils.isNotEmpty( jwt )){
            response.setStatusCode( HttpStatus.UNAUTHORIZED );
            return response.setComplete();
        }
        //4.对当前的请求对象进行增强，让其携带令牌信息
        request.mutate().header( "Authorization","Bearer "+jwt );

        return chain.filter( exchange );
    }


    @Override
    public int getOrder() {
        return 0;
    }
}
